let express = require('express');
let router = express.Router();
let db = require('../tools/DB');
let to = require('../tools/to');
let intercept = require('../tools/intercept')



//列表页相关路由

router.post('/course', async (req, res) => {
    let {
        type = 1, page = 1, pagesize = 9, area_name = '', grade_name = '', subject_name = '', type_name = ''
    } = req.body;
    let pageNum = (page - 1) * pagesize;
    let sql = "select id,cid,title,price,concat('/uploads/course/',image_src) as image_src,type_name,area_name,subject_name,grade_name,category_name from e_course where type=? ";
    let sql2 = 'select count(id) as totalNum  from e_course where type=? '
    let str = '';
    if (area_name != '') {
        str += `and area_name='${area_name}' `
    }
    if (grade_name != '') {
        str += `and grade_name='${grade_name}' `
    }
    if (subject_name != '') {
        str += `and subject_name='${subject_name}' `
    }
    if (type_name != '') {
        str += `and type_name='${type_name}' `
    }
    sql += str + 'limit ?,?';
    sql2 += str;
    let [err, result] = await to(db.query(sql, [type, Number(pageNum), Number(pagesize)]));
    let [{
        totalNum
    }] = await db.query(sql2, [type]);

    //需要把刚才获取到的数据存在一个对象中，成功的时候传上去
    let data = {
        page,
        pagesize,
        totalNum,
        result
    }
    if (!err) {
        res.json({
            code: 200,
            msg: '成功',
            data
        });
    } else {
        res.json({
            code: 200,
            msg: '失败',
            data: err
        });
    }



});


//地区列表接口
router.get('/areas', async (req, res) => {
    let sql = 'select * from e_area';
    let result = await db.query(sql, []);
    res.json({
        code: 200,
        msg: '成功',
        data: result
    });
})

//年级
router.get('/grades', async (req, res) => {
    let sql = 'select * from e_grade';
    let result = await db.query(sql, []);
    res.json({
        code: 200,
        msg: '成功',
        data: result
    });
})

//学科
router.get('/subjects', async (req, res) => {
    let sql = 'select * from e_subject';
    let result = await db.query(sql, []);
    res.json({
        code: 200,
        msg: '成功',
        data: result
    });
})

//考试类别
router.get('/types', async (req, res) => {
    let sql = 'select * from e_type';
    let result = await db.query(sql, []);
    res.json({
        code: 200,
        msg: '成功',
        data: result
    });
})


//课程详情
router.get("/coursedetail", async (req, res) => {
    let {
        cid
    } = req.query;
    let sql1 = `SELECT
e_course.area_name,e_course.image_src,e_course.title,e_course.subject_name,e_course.intro as courseIntro,e_course.price,
e_teacher.head_photo_url,e_teacher.intro AS teacherIntro,e_teacher.realname
FROM
e_course
INNER JOIN e_teacher ON e_course.tid = e_teacher.tid
WHERE
e_course.cid = ?;`

    let sql2 = `select * from e_video where cid=?`;

    Promise.all([
        db.query(sql1, [cid]),
        db.query(sql2, [cid]),
    ]).then((response) => {
        //console.log(data);
        let [courseDetailData, videoDetailData] = response;
        let data = courseDetailData[0];
        data.videoDetail = videoDetailData;
        res.json({
            code: 200,
            data,
            msg: "课程详情获取成功"
        })
    }).catch((err) => {
        //console.log(err);
        res.json({
            code: 208,
            data: err,
            msg: "课程详情获取失败"
        })
    })
})

router.use(intercept());
//视频播放接口
router.get('/videoplay/:id',async (req,res)=>{
    let id = req.params.id;
    let sql = "select id,concat('/uploads/video/',video_src) as video_src,video_title from e_video where id=?";
    let[err,result] =await to(db.query(sql,[id]));
    if(!err){
        res.json({code:200,data:result,msg:"成功"})
    }else{
        res.json({code:500,data:err,msg:"失败"})
    };
})


module.exports = router;